package com.lsp.data.net;

import com.lsp.data.Constants;
import com.lsp.data.bean.GankInfoList;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.HTTP;
import retrofit2.http.Path;
import rx.Observable;

/**
 * @author com.lsp.ibxs
 * @created 16/11/8 21:26
 */
public interface RetrofitManager {

  /**
   * 获取gank数据
   */
  //@GET("http://gank.io/api/data/Android/10/1") Observable<List<String>> getGankInfo(
  //    @QueryMap Map<String, String> map, @Query(value = "d", encoded = true) String d,
  //    @Query(value = "u", encoded = true) String u);
  @HTTP(method = "GET", path = "data/Android/{date}", hasBody = false)
  Observable<GankInfoList> getGankInfo(@Path("date") String date);

  class Creator {
    public static RetrofitManager create() {
      //log的拦截器
      HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
      interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
      //设置okhttpClient
      OkHttpClient.Builder httpClient = new OkHttpClient.Builder().retryOnConnectionFailure(true)
          .connectTimeout(20, TimeUnit.SECONDS)
          .addInterceptor(interceptor);
      OkHttpClient client = httpClient.build();
      //构建retrofit
      Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.URL.BASE_URL)//设置baseurl
          .addConverterFactory(GsonConverterFactory.create())
          .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
          .client(client)
          .build();
      return retrofit.create(RetrofitManager.class);
    }
  }
}
